package com.sicpa.web.controller.system;

import com.sicpa.common.annotation.Log;
import com.sicpa.common.constant.Constants;
import com.sicpa.common.core.domain.AjaxResult;
import com.sicpa.common.core.domain.entity.SysUser;
import com.sicpa.common.enums.BusinessType;
import com.sicpa.common.utils.SecurityUtils;
import com.sicpa.vo.ForgetPwd;
import com.sicpa.common.core.redis.RedisCache;
import com.sicpa.common.utils.RandomNumCode;
import com.sicpa.common.utils.uuid.IdUtils;
import com.sicpa.system.service.ISendMobileMessageService;
import com.sicpa.system.service.ISysUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.constraints.NotBlank;
import java.io.UnsupportedEncodingException;
import java.util.concurrent.TimeUnit;

/**
 * ForgetController
 *
 * @author lwf
 */
@RestController
@RequestMapping("/forget")
public class ForgetController {

    private ISendMobileMessageService iSendMobileMessageService;

    private RedisCache redisCache;

    private ISysUserService iSysUserService;

    @Autowired
    public ForgetController(ISendMobileMessageService iSendMobileMessageService, RedisCache redisCache, ISysUserService iSysUserService) {
        this.iSendMobileMessageService = iSendMobileMessageService;
        this.redisCache = redisCache;
        this.iSysUserService = iSysUserService;
    }

    /**
     * 忘记密码--发送短信验证码
     *
     * @param forgetPwd
     * @return
     */
    @ApiOperation("忘记密码发送短信接口")
    @ApiImplicitParam(name = "forgetPwd", value = "忘记密码实体类", dataType = "ForgetPwd")
    @GetMapping(value = "/sendMobleCode")
    public AjaxResult sendMobileMessage(@Validated ForgetPwd forgetPwd) throws UnsupportedEncodingException {
        String uuid = forgetPwd.getUuid();
        String account = forgetPwd.getAccount();
        String code = forgetPwd.getCode();
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        String captcha = redisCache.getCacheObject(verifyKey);
        if (captcha == null) {
            return AjaxResult.error("验证码不存在!");
        }
        if (!code.equalsIgnoreCase(captcha)) {
            return AjaxResult.error("验证码错误!");
        }
        redisCache.deleteObject(verifyKey);
        SysUser sysUser = iSysUserService.selectUserByUserName(account);
        if (sysUser == null) {
            return AjaxResult.error("账号不存在!");
        }
        String phonenumber = sysUser.getPhonenumber();
        String userName = sysUser.getUserName();
        String mobileCode = RandomNumCode.generateNumCode();
        iSendMobileMessageService.sendMobileMessageGet(" 您的手机验证码是" + mobileCode + ",请在3分钟内按页面提示提交验证码!", phonenumber);
        String vuuid = IdUtils.simpleUUID();
        forgetPwd.setAccount(userName);
        forgetPwd.setUuid(vuuid);
        forgetPwd.setCode(mobileCode);
        redisCache.setCacheObject(Constants.CAPTCHA_CODE_KEY + vuuid, forgetPwd, Constants.MOBILE_EXPIRATION, TimeUnit.MINUTES);
        return AjaxResult.success(vuuid);
    }

    /**
     * 忘记密码--重置密码
     *
     * @return
     */
    @Log(title = "忘记密码--重置密码", businessType = BusinessType.UPDATE)
    @ApiOperation("忘记密码重置密码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="uuid",value="uuid,短信发送接口返回",required=true,paramType="query"),
            @ApiImplicitParam(name="verifyCode",value="短信验证码",required=true,paramType="query"),
    })
    @GetMapping(value = "/resetPwd")
    public AjaxResult resetPwd(@Validated @NotBlank(message = "uuid不能为空") @RequestParam("uuid") String uuid,@Validated @NotBlank(message = "验证码不能为空")  @RequestParam("verifyCode") String verifyCode,@Validated @NotBlank(message = "新密码不能为空")  @RequestParam("resetPwd") String resetPwd) {
        String verifyKey = Constants.CAPTCHA_CODE_KEY + uuid;
        ForgetPwd cacheForgetPwd = redisCache.getCacheObject(verifyKey);
        if(cacheForgetPwd==null){
            return AjaxResult.error("验证key错误!");
        }
        String cacheCode = cacheForgetPwd.getCode();
        if(!cacheCode.equalsIgnoreCase(verifyCode)){
            return AjaxResult.error("验证码错误!");
        }
        redisCache.deleteObject(verifyKey);
        return AjaxResult.success(iSysUserService.resetUserPwd(cacheForgetPwd.getAccount(), SecurityUtils.encryptPassword(resetPwd)));
    }
}
